/**
 * RedBlueColourScheme.java
 * colourmap
 * Copyright (C) 2010 Alan W.F. Boyd
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * You can contact the author via the google code web page for the project
 * <http://colourmap.googlecode.com>
 */
package com.googlecode.colourmap.colours;

import java.awt.Color;

import com.googlecode.colourmap.ColourSchemeKey;


/**
 * Provides a mapping of values to colours.
 * The lowest data values are coloured in blue, while the highest values are coloured in red.
 * Between those points, data values are interpolated.
 * 
 * @author Alan W.F. Boyd
 */
public class RedBlueColourScheme implements IColourScheme
{
	/** The minimum data value */
	private float min = 0;
	
	/** The maximum data value */
	private float max = 0;
	
	/** A key to show the user the mapping from colours to data values */
	private ColourSchemeKey k;
	
	/**
	 * The constructor initialises the key and makes it visible to the user.
	 */
	public RedBlueColourScheme()
	{
		k = new ColourSchemeKey(min, max);
		k.setColourScheme(this);
		k.setVisible(true);
	}
	
	/**
	 * This function returns the colour of the outline of data values.
	 * @return the colour of the data value outlines
	 */
	public Color getOutlineColour()
	{
		return Color.black;
	}
	
	/**
	 * Given a data value, this function returns the colour of that value
	 * @param f the data value
	 * @return the colour associated with that data value
	 */
	public Color getColour(float f)
	{
		if (f <= min)
			return new Color(0, 0, 255);
		else if (f >= max)
			return new Color(255, 0, 0);
		
		// Find out at what point between the max and min this value lies
		float maxDifference = max - min;
		float difference = f - min;
		int magnitude = (int)(difference*1020/maxDifference);
		
		int col = magnitude / 255;
		int str = magnitude % 255;
		
		if (col == 0)
			return new Color(0, str, 255);
		else if (col == 1)
			return new Color(0, 255, 255-str);
		else if (col == 2)
			return new Color(str, 255, 0);
		else if (col == 3)
			return new Color(255, 255-str, 0);
		
		return new Color(255, 0, 0);
	}

	/**
	 * Sets the maximum data value from the data set
	 * @param f the new maximum data value
	 */
	public void setMax(float f)
	{
		max = f;
		
		k.setMax(f);
		k.repaint();
	}

	/**
	 * Sets the minimum data value from the data set
	 * @param f the new minimum data value
	 */
	public void setMin(float f)
	{
		min = f;
		k.setMin(f);
		k.repaint();
	}
}
